//
//  UIViewController+WDNavigitionItem.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/11/28.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "UIViewController+WDNavigitionItem.h"

#define NavFont 18

@implementation UIViewController (WDNavigitionItem)

- (void)creatTitleView:(NSString *)imageName imageSize:(CGSize)imageSize
{
    UIImageView *titleView = [[UIImageView alloc] init];
    titleView.image = [UIImage imageWithOriginal:imageName];
    titleView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
    self.navigationItem.titleView = titleView;
}

- (void)setNavLeftBtnWithAction:(SEL)leftAction leftBtnImage:(NSString *)leftBtnImage leftBtnSelImage:(NSString *)leftBtnSelImage
{
    [self creatBarButtonWithBtnAction:leftAction btnImage:leftBtnImage btnSelectedImage:leftBtnSelImage flog:0];
}

/** flog 0 = left, 1 = right */
- (void)creatBarButtonWithBtnAction:(SEL)btnAction btnImage:(NSString *)btnImage btnSelectedImage:(NSString *)btnSelectedImage flog:(BOOL)flog
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageWithOriginal:btnImage] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageWithOriginal:btnSelectedImage] forState:UIControlStateSelected];
    [btn addTarget:self action:btnAction forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, WDScale(30), WDScale(30));
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    if (!flog) {//导航栏左边按钮
        
        UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        
        spacer.width = -10;
        
        self.navigationItem.leftBarButtonItems = @[spacer, item];
    } else {//导航栏右边按钮
        
        UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        
        spacer.width = -10;
        
        self.navigationItem.rightBarButtonItems = @[spacer, item];
    }
    
}

- (void)loadNavItemWithAction:(SEL)action btnImage:(NSString *)btnImage btnSelImage:(NSString *)btnSelImage title:(NSString *)title
{
    //左边按钮
    [self creatBarButtonWithBtnAction:action btnImage:btnImage btnSelectedImage:btnSelImage flog:0];
    
    //中间标题
    self.navigationItem.title = title;
}

- (void)loadNavItemWithLeftAction:(SEL)leftAction leftBtnImage:(NSString *)leftBtnImage leftBtnSelImage:(NSString *)leftBtnSelImage title:(NSString *)title rihgtAction:(SEL)rightAction rightBtnImage:(NSString *)rightBtnImage rightBtnSelImage:(NSString *)rightBtnSelImage
{
    //左边按钮
    [self creatBarButtonWithBtnAction:leftAction btnImage:leftBtnImage btnSelectedImage:leftBtnSelImage flog:0];
    
    //中间标题
    self.navigationItem.title = title;
    
    //右边按钮
    [self creatBarButtonWithBtnAction:rightAction btnImage:rightBtnImage btnSelectedImage:rightBtnSelImage flog:1];
}

- (void)loadNavItemWithLeftAction:(SEL)leftAction leftBtnImage:(NSString *)leftBtnImage leftBtnSelImage:(NSString *)leftBtnSelImage title:(NSString *)title rihgtAction:(SEL)rightAction rightStr:(NSString *)rightStr
{
    //左边按钮
    [self creatBarButtonWithBtnAction:leftAction btnImage:leftBtnImage btnSelectedImage:leftBtnSelImage flog:0];
    
    //中间标题
    self.navigationItem.title = title;
    
    //右边文字
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:rightStr forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:NavFont];
    [rightBtn addTarget:self action:rightAction forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, 50, 30);
    
    UIBarButtonItem *itemR = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    spacer.width = -10;
    
    [self.navigationItem setRightBarButtonItems:@[spacer, itemR]];
}

- (void)loadNavItemWithLeftAction:(SEL)leftAction leftBtnStr:(NSString *)leftStr  title:(NSString *)title rihgtAction:(SEL)rightAction rightStr:(NSString *)rightStr
{
    //左边按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:leftStr forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:NavFont];
    [leftBtn addTarget:self action:leftAction forControlEvents:UIControlEventTouchUpInside];
    leftBtn.frame = CGRectMake(0, 0, 50, 30);
    
    UIBarButtonItem *itemL = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    spacer.width = -10;
    
    [self.navigationItem setLeftBarButtonItems:@[spacer, itemL]];
    
    
    //中间标题
    self.navigationItem.title = title;
    
    //右边按钮
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:rightStr forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:NavFont];
    [rightBtn addTarget:self action:rightAction forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, 50, 30);
    
    UIBarButtonItem *itemR = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *spacerR = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    spacerR.width = -10;
    
    [self.navigationItem setRightBarButtonItems:@[spacerR, itemR]];
    
}


@end
